// 创建二维码容器
function createQRContainer() {
  // 创建样式
  const style = document.createElement("style");
  style.textContent = `
    #qr-container {
      position: fixed;
      right: 20px;
      bottom: 20px;
      z-index: 999999;
    }
    #qr-toggle {
      width: 40px;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 50%;
      cursor: pointer;
      background: #4a90e2;
      color: white;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
      transition: transform 0.2s;
    }
    #qr-toggle:hover {
      transform: scale(1.1);
    }
    #qr-toggle-icon {
      width: 24px;
      height: 24px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    #qr-content {
      display: none;
      position: absolute;
      bottom: 48px;
      right: 0;
      padding: 16px;
      padding-top: 20px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    }
    #qr-container.expanded #qr-content {
      display: block;
    }
    .qr-button {
      position: absolute;
      top: -10px;
      width: 24px;
      height: 24px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      color: #666;
      background: #fff;
      border-radius: 50%;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
      transition: all 0.2s;
      font-size: 16px;
      z-index: 10;
    }
    .qr-button:hover {
      transform: scale(1.1);
      background: #f5f5f5;
      color: #333;
    }
    #qr-close {
      right: -8px;
      font-size: 18px;
      font-weight: bold;
    }
    #qr-download {
      right: 24px;
      font-size: 16px;
    }
    .qr-button:active {
      transform: scale(0.95);
      background: #ebebeb;
    }
    #qr-code {
      width: 160px;
      height: 160px;
      margin-top: 8px;
    }
    #qr-code img {
      display: block;
      max-width: 100%;
    }
    #qr-logo {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 32px;
      height: 32px;
      padding: 4px;
      background: #fff;
      border-radius: 4px;
    }
    #site-info {
      margin-top: 8px;
      text-align: center;
      color: #666;
      font-size: 12px;
      line-height: 1.4;
    }
    .site-name {
      font-weight: bold;
      margin-bottom: 2px;
      color: #333;
    }
  `;
  document.head.appendChild(style);

  // 创建容器
  const container = document.createElement("div");
  container.id = "qr-container";

  // 创建二维码内容区域
  const qrContent = document.createElement("div");
  qrContent.id = "qr-content";

  // 创建二维码区域
  const qrCode = document.createElement("div");
  qrCode.id = "qr-code";

  // 创建网站信息区域
  const siteInfo = document.createElement("div");
  siteInfo.id = "site-info";

  // 创建按钮
  const toggleButton = document.createElement("div");
  toggleButton.id = "qr-toggle";
  const toggleIcon = document.createElement("div");
  toggleIcon.id = "qr-toggle-icon";

  // 尝试加载网站favicon
  getFavicon().then((favicon) => {
    if (favicon) {
      const img = new Image();
      img.onload = () => {
        toggleIcon.style.backgroundImage = `url('${favicon}')`;
        toggleIcon.style.backgroundSize = "contain";
        toggleIcon.style.backgroundPosition = "center";
        toggleIcon.style.backgroundRepeat = "no-repeat";
      };
      img.onerror = () => {
        toggleIcon.innerHTML = `<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9h6V3H3v6zm2-4h2v2H5V5zm8 8h6v-6h-6v6zm2-4h2v2h-2v-2zM3 21h6v-6H3v6zm2-4h2v2H5v-2zm13-2h3v2h-3v3h-2v-3h-3v-2h3v-3h2v3z"/></svg>`;
      };
      img.src = favicon;
    } else {
      toggleIcon.innerHTML = `<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9h6V3H3v6zm2-4h2v2H5V5zm8 8h6v-6h-6v6zm2-4h2v2h-2v-2zM3 21h6v-6H3v6zm2-4h2v2H5v-2zm13-2h3v2h-3v3h-2v-3h-3v-2h3v-3h2v3z"/></svg>`;
    }
  });

  toggleButton.appendChild(toggleIcon);

  // 创建关闭和下载按钮
  const closeButton = document.createElement("div");
  closeButton.id = "qr-close";
  closeButton.className = "qr-button";
  closeButton.innerHTML = "×";

  const downloadButton = document.createElement("div");
  downloadButton.id = "qr-download";
  downloadButton.className = "qr-button";
  downloadButton.innerHTML = "⭳";

  // 组装DOM
  qrContent.appendChild(closeButton);
  qrContent.appendChild(downloadButton);
  qrContent.appendChild(qrCode);
  qrContent.appendChild(siteInfo);
  container.appendChild(qrContent);
  container.appendChild(toggleButton);
  document.body.appendChild(container);

  // 添加点击事件
  toggleButton.addEventListener("click", () => {
    if (container.className !== "expanded") {
      container.className = "expanded";
      if (!qrCode.hasChildNodes()) {
        generateQRCode(qrCode, siteInfo);
      }
    }
  });

  closeButton.addEventListener("click", (e) => {
    e.stopPropagation();
    container.className = "";
  });

  downloadButton.addEventListener("click", (e) => {
    e.stopPropagation();
    if (qrInstance) {
      downloadQRCode();
    }
  });

  return { container, qrCode, siteInfo };
}

// 获取网站logo
async function getFavicon() {
  const favicon =
    document.querySelector('link[rel="icon"]') ||
    document.querySelector('link[rel="shortcut icon"]');
  return favicon ? favicon.href : "";
}

// 修改generateQRCode函数，保存QRCode实例
let qrInstance = null;

function generateQRCode(qrCode, siteInfo) {
  try {
    // 创建QRCode实例
    qrInstance = new QRCode(qrCode, {
      text: window.location.href,
      width: 160,
      height: 160,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });

    // 添加网站信息
    const siteName = document.createElement("div");
    siteName.className = "site-name";
    siteName.textContent = document.domain;

    const pageTitle = document.createElement("div");
    pageTitle.className = "page-title";
    pageTitle.textContent = document.title.substring(0, 15);

    siteInfo.appendChild(siteName);
    siteInfo.appendChild(pageTitle);

    // 添加网站logo
    getFavicon().then((favicon) => {
      if (favicon) {
        const logo = document.createElement("img");
        logo.src = favicon;
        logo.id = "qr-logo";
        qrCode.appendChild(logo);
      }
    });
  } catch (error) {
    console.error("生成二维码时出错:", error);
  }
}

// 下载二维码
function downloadQRCode() {
  const qrImage = qrInstance._oDrawing._elImage;
  const link = document.createElement("a");
  link.href = qrImage.src;
  link.download = `${document.title.substring(0, 15)}_QR_Code.png`;
  link.click();
}

// 等待页面加载完成后执行
window.addEventListener("load", () => {
  // 注入QRCode库的代码
  const qrcodeScript = document.createElement("script");
  qrcodeScript.textContent = `
    ${QRCode.toString()}
    window.QRCode = QRCode;
  `;
  document.head.appendChild(qrcodeScript);

  // 创建容器
  createQRContainer();
});
